package com.lv.suyiyong.hfrbusines.repo

import androidx.lifecycle.LiveData
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.lv.common.model.SingleLiveData
import com.lv.common.network.model.DataResult
import com.lv.common.network.support.serverData
import com.lv.common.utils.showToast
import com.lv.service.bean.SpuInfoBean
import com.lv.service.network.*
import com.lv.suyiyong.hfrbusines.bean.VersionListBean
import com.lv.suyiyong.hfrbusines.net.MainService
import retrofit2.http.QueryMap

/**
 * 创建者：ygx
 * 创建日期：2021/9/17 11:28 PM
 * 描述：TODO
 */
class MainRepo(private val service: MainService) : IMainResource {

    /**
     * 刷新Token
     */
    override suspend fun refreshtoken(
    ): DataResult<BaseYgxRsp> {
        return service.refreshtoken().serverData()
    }



    private val _versionUpDateRsp = SingleLiveData<VersionListBean>()
    override val versionUpDateRsp: LiveData<VersionListBean> = _versionUpDateRsp
    override suspend fun findVersionUpdate(
    ) {
        service.findVersionUpdate()
            .serverData()
            .onSuccess {
                //只要不是接口响应成功
                onBizError<Any> { code, message, data ->
//                    LogUtils.e("更新接口 BizError $code ,$message")
                }
                onBizOK<VersionListBean> { code, data, message, result ->
                    _versionUpDateRsp.value = data
//                    LogUtils.e("更新接口 Bizok $data")
                    return@onBizOK
                }
            }
            .onFailure {
                LogUtils.e("更新接口 接口异常 ${it.message}")
                showToast("服务器异常")
                ActivityUtils.finishAllActivities()
            }
    }

//    private val _findProsDateRsp = SingleLiveData<FindProsBean>()
//    override val findProsDateRsp: LiveData<FindProsBean> = _findProsDateRsp
//    override suspend fun findPros(
//        map: Map<String, String>
//    ) {
//        service.findPros(map)
//            .serverData()
//            .onSuccess {
//                //只要不是接口响应成功
//                onBizError<FindProsBean> { code, message, data ->
////                    LogUtils.e("接口 BizError $code ,$message ,$data")
//                }
//                onBizOK<FindProsBean> { code, data, message, result ->
//                    _findProsDateRsp.value = data
////                    LogUtils.e("接口 Bizok $data")
//                    return@onBizOK
//                }
//            }
//            .onFailure {
//                LogUtils.e("接口 接口异常 ${it.message}")
//            }
//    }

    /**
     * 商品相关基本信息
     */
    private val _spuCategoryDateRsp = SingleLiveData<SpuInfoBean>()
    override val spuCategoryDateRsp: LiveData<SpuInfoBean> = _spuCategoryDateRsp
    override suspend fun spuCategory(

    ) {
        service.spuCategory()
            .serverData()
            .onSuccess {
                //只要不是接口响应成功
                onBizError<Any> { code, message, data ->
//                    LogUtils.e("spuCategory接口 BizError $code ,$message ,$data")
                }
                onBizOK<SpuInfoBean> { code, data, message, result ->
                    _spuCategoryDateRsp.value = data
//                    LogUtils.e("spuCategory接口 Bizok $data")
                    return@onBizOK
                }
            }
            .onFailure {
                LogUtils.e("spuCategory接口 接口异常 ${it.message}")
            }
    }

    /**
     * 推送安卓
     */
    override suspend fun saveTX(@QueryMap map: Map<String, String>): DataResult<BaseYgxRsp> {
        return service.saveTX(map).serverData()
    }
}